﻿Imports PTIC_Util

Public Class VMGrilleCompte
    Inherits BaseNotifyPropertyChangeComponentModel

#Region "Constructeur"
    Public Sub New()
        InitTitresTableContenu()
    End Sub
#End Region

#Region "Gestion de la table de contenu"
    Private _TableContenuTampon As New DataTable
    Protected Property TableContenuTampon As DataTable
        Get
            Return _TableContenuTampon
        End Get
        Set(ByVal value As DataTable)
            _TableContenuTampon = value
        End Set
    End Property

    Private _TableContenu As DataTable
    Public ReadOnly Property TableContenu As DataTable
        Get
            Return _TableContenu
        End Get
    End Property
    Public Event TableContenuChanging As PropertyChangeEventHandler(Of DataTable)
    Public Event TableContenuChanged As PropertyChangeEventHandler(Of DataTable)

    Protected Sub MAJTableContenu()
        Dim TValeurs As DataTable = _TableContenuTampon.Copy
        _TableContenu = TValeurs
        OnPropertyChanged("TableContenu")
    End Sub
    Protected Overridable Sub InitTitresTableContenu()
    End Sub
#End Region

    'Gestion de la première année de projection
    'Lorsque le datacontext est créé, il contient un objet compte qui contient une propriété 1ère année de projection
    'A chaque fois que la 1ère année de projection du compte change, il faut générer un évènement onPropertyChanged dans le ViewModel

#Region "Objet métier contenant les comptes"
    Public Property Comptes As Comptes.ComptesMoteur2
        Get
            Return _Comptes
        End Get
        Set(ByVal value As Comptes.ComptesMoteur2)
            If Not value Is Nothing Then
                OnPropertyChanging("Comptes")
                Dim e As New PropertyChangeEventArgs(Of Comptes.ComptesMoteur2)(Comptes, value)
                If e.IsValide Then RaiseEvent ComptesChanging(Me, e)
                If e.IsValide Then
                    If Not _Comptes Is Nothing Then
                        RemoveHandler _Comptes.NbAnneesProjectionChanged, AddressOf NombreAnneesProjection_Changed
                        RemoveHandler _Comptes.PremiereAnneeProjectionChanged, AddressOf PremiereAnneeProjection_Changed
                        RemoveHandler _Comptes.HasPMdc_Changed, AddressOf HasPM_Changed
                        RemoveHandler _Comptes.HasPMrente_Changed, AddressOf HasPM_Changed
                        RemoveHandler _Comptes.HasPPRC_Changed, AddressOf HasPM_Changed
                        RemoveHandler _Comptes.AfterCalculer, AddressOf HasBeenCalculated
                        RemoveHandler _Comptes.Unite_Changed, AddressOf Unite_Changed
                    End If
                    _Comptes = e.NewValue
                    AddHandler _Comptes.NbAnneesProjectionChanged, AddressOf NombreAnneesProjection_Changed
                    AddHandler _Comptes.PremiereAnneeProjectionChanged, AddressOf PremiereAnneeProjection_Changed
                    AddHandler _Comptes.HasPMdc_Changed, AddressOf HasPM_Changed
                    AddHandler _Comptes.HasPMrente_Changed, AddressOf HasPM_Changed
                    AddHandler _Comptes.HasPPRC_Changed, AddressOf HasPM_Changed
                    AddHandler _Comptes.AfterCalculer, AddressOf HasBeenCalculated
                    AddHandler _Comptes.Unite_Changed, AddressOf Unite_Changed
                    OnPropertyChanged("Comptes")
                    RaiseEvent ComptesChanged(Me, e)
                End If
            End If
        End Set
    End Property
    Private WithEvents _Comptes As Comptes.ComptesMoteur2
    Public Event ComptesChanging As PropertyChangeEventHandler(Of Comptes.ComptesMoteur2)
    Public Event ComptesChanged As PropertyChangeEventHandler(Of Comptes.ComptesMoteur2)
    Public Sub Comptes_Changed(ByVal sender As Object, ByVal e As EventArgs) Handles Me.ComptesChanged
        refreshAll()
    End Sub
#End Region

    Private Sub Unite_Changed(ByVal sender As Object, ByVal e As EventArgs)
        RefreshLignes(TableContenu, True)
    End Sub

    Private Sub NombreAnneesProjection_Changed(ByVal sender As Object, ByVal e As EventArgs)
        'refreshAll()
    End Sub
    Private Sub PremiereAnneeProjection_Changed(ByVal sender As Object, ByVal e As EventArgs)
        OnPropertyChanged("TableContenu")
    End Sub
    Private Sub HasPM_Changed(ByVal sender As Object, ByVal e As EventArgs)
        RefreshLignes(TableContenu, True)
    End Sub
    Private Sub HasBeenCalculated(ByVal sender As Object, ByVal e As EventArgs)
        refreshAll()
    End Sub

    Protected Overridable Sub refreshAll()
        Dim ajColonnes As Boolean = False
        If Comptes.NombreAnneesProjection <> TableContenu.Columns.Count - 1 Then
            If TableContenu.Columns.Count >= 2 Then
                If CInt(TableContenu.Columns(1).ColumnName) <> Comptes.AnneePremiereGeneration Then
                    RefreshColonnes(False)
                    ajColonnes = True
                End If
            Else
                RefreshColonnes(False)
                ajColonnes = True
            End If
        End If
        If ajColonnes Then
            RefreshLignes(TableContenuTampon, False)
            MAJTableContenu()
        Else
            RefreshLignes(TableContenu, True)
        End If
    End Sub
    Protected Overridable Sub RefreshLignes(ByVal TValeurs As DataTable, Optional ByVal RaisePropChangedEvent As Boolean = False)
    End Sub
    Protected Sub RefreshColonnes(Optional ByVal TableContenuMAJ As Boolean = False)
        Dim i As Integer = Comptes.NombreAnneesProjection
        Dim colListe As List(Of String) = New List(Of String)
        For Each col As DataColumn In TableContenuTampon.Columns
            If col.ColumnName <> "ComptesHeaders" Then
                colListe.Add(col.ColumnName)
            End If
        Next
        'Faire un test pour vérifier que les colonnes supprimées ne sont pas dans la fourchette AnneeDeb - AnneeDeb+nbanneesProj
        For Each colName As String In colListe
            TableContenuTampon.Columns.Remove(colName)
        Next
        For i = 0 To Comptes.NombreAnneesProjection - 1
            TableContenuTampon.Columns.Add(i, GetType(String)).Caption = "Test"
        Next
        If TableContenuMAJ Then MAJTableContenu()
    End Sub

End Class
